<template>
  <a-drawer
    :title="title"
    placement="right"
    :closable="true"
    :width="width"
    :visible="visible"
    @close="handleCancel"
    destroyOnClose
  >
    <div>
      <div class="table-page-search-wrapper">
        <a-form layout="inline" @keyup.enter.native="searchQuery">
          <a-row :gutter="24">
            <a-col :span="24">
              <a-form-item label="产品名称">
                <a-input placeholder="请输入产品名称" v-model="queryParam.productName"></a-input>
              </a-form-item>
            </a-col>
            <a-col :span="24">
              <a-form-item label="产品类型">
                <a-tree-select
                  allowClear
                  v-model="queryParam.productType"
                  style="width: 100%"
                  :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
                  :tree-data="treeData"
                  :replaceFields="dataFields"
                  placeholder="请选择类型"
                  @change="changeParentId"
                >
                </a-tree-select>
              </a-form-item>
            </a-col>
            <a-col :span="12">
              <a-button type="primary" @click="searchQuery" icon="search" style="width: 100%">查询</a-button>
            </a-col>
            <a-col :span="12">
              <a-button type="primary" @click="searchReset" icon="reload" style="width: 100%">重置</a-button>
            </a-col>
          </a-row>
        </a-form>
      </div>
      <!-- 查询区域-END -->

      <div>
        <a-button type="primary" @click="handleOk" style="width: 100%;margin-bottom: 10px;margin-top: 10px">添加到套餐</a-button>
        <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
          <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{
          selectedRows.length }}</a>项
          <a style="margin-left: 24px" @click="onClearSelected">清空</a>
        </div>

        <div class="list">
          <van-list
            v-model="loading"
            :finished="finished"
            finished-text="没有更多了"
            @load="onLoadList"
          >
            <div
              class="customer-list"
              v-for="(item,i) in dataSource"
              :key="i">
              <div>
                <div class="name">{{item.productName || ''}}</div>
                <div class="contact">
                  <span>产品售价：{{ item.productPrice || '无' }}</span>
                  <span style="float:right;">
                    <a-checkbox @click.stop="checkClick(item,$event)"></a-checkbox>
                  </span>
                </div>
                <div class="contact">
                  <span>单位：{{ item.unit || '无'}}</span>
                </div>
              </div>
            </div>
          </van-list>
        </div>
      </div>
    </div>
  </a-drawer>

</template>

<script>
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import {getAction} from '@/api/manage'

  export default {
    name: 'selectProductModal',
    mixins:[JeecgListMixin, mixinDevice],
    data () {
      return {
        description: '产品管理表管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'产品名称',
            align:"center",
            dataIndex: 'productName'
          },
          {
            title:'产品类型',
            align:"center",
            dataIndex: 'productTypeName',
          },
          {
            title:'产品售价',
            align:"center",
            dataIndex: 'productPrice'
          },
          {
            title:'单位',
            align:"center",
            dataIndex: 'unit'
          },
        ],
        url: {
          list: "/ghProductManage/list",
          getParent: "/ghProductType/getProductParent"
        },
        treeData: [],
        dataFields: {
          title: 'productType',
          key: 'id',
          value: 'id',
          children: "children"
        },
        dictOptions:{},
        selectedRowKeys: [],
        selectedRows: [],
        dataSource:[],
        title:"",
        width:"100%",
        visible:false,
        finished: false,
      }
    },
    created() {

    },
    methods:{
      //下一页
      onLoadList(){
        this.ipagination.current++
        this.loadData(this.ipagination.current)
      },
      loadData(arg){
        //加载数据 若传入参数1则加载第一页的内容
        if (arg === 1) {
          this.ipagination.current = 1;
          this.dataSource = [];
          this.finished = false;
        }
        let params = this.getQueryParams();//查询条件
        this.loading = true
        getAction(this.url.list, params).then((res) => {
          if (res.success) {
            this.dataSource = [...this.dataSource, ...res.result.records]
            if (res.result.total) {
              this.ipagination.total = res.result.total
            } else {
              this.ipagination.total = 0
            }
          } else {
            this.$message.warning(res.message)
          }
          this.loading = false
          if(res.result.records.length<10){
            this.finished = true;
          }
        }).finally(() => {
          this.loading = false
        })
      },
      onSelectAll(selected, selectedRows, changeRows) {
        if(selected) {
          let rows = [...selectedRows]
          this.selectedRows = this.selectedRows.concat(rows)
        } else {
          changeRows.forEach(item => {
            const delIndex = this.selectedRows.findIndex((val) => {
              return val.id === item.id
            })
            this.selectedRows.splice(delIndex, 1)
          })
        }
      },
      onSelect(record, selected) {
        if(selected) {
          this.selectedRows.push(record);
        } else {
          const delIndex = this.selectedRows.findIndex((val) => {
            return val.id === record.id
          })
          this.selectedRows.splice(delIndex,1)
        }
      },
      onSelectChange(selectedRowKeys) {
        this.selectedRowKeys = selectedRowKeys;
      },
      onClearSelected() {
        this.selectedRowKeys = [];
        this.selectedRows = [];
      },
      changeParentId(value){
        this.loadData(1);
      },
      handleCancel(){
        this.visible = false;
        this.queryParam = {};
        this.onClearSelected();
      },
      handleOk(){
        if (this.selectedRows.length === 0){
          this.$message.warning("请至少选择一条数据")
          return;
        }
        let arr = Array.from(new Set(this.selectedRows));
        console.log(arr)
        this.$emit("selectOk",this.selectedRows);
        this.handleCancel()
      },
      initTreeData() {
        getAction(this.url.getParent).then(res => {
          if (res.success) {
            this.treeData = res.result;
          }
        });
      },
      checkClick(record,e){
        if (e.target.checked){
          this.selectedRows.push(record)
          this.selectedRowKeys.push(record.id)
        }else {
          const delIndex1 = this.selectedRows.findIndex((val) => {
            return val.id === record.id;
          })
          const delIndex2 = this.selectedRowKeys.findIndex((val) => {
            return val === record.id;
          })
          this.selectedRows.splice(delIndex1,1)
          this.selectedRowKeys.splice(delIndex2,1)
        }
      },
    },
  }
</script>

<style scoped lang="less">
  @import '~@assets/less/common.less';
  .select-pk{
    display:flex;
    align-items:center;
    margin:10px 0;
    &>span{
      font-weight: bold;
    }
    &-text{
      width: calc(100% - 6em);
      height: 30px;
      text-indent: 1em;
      line-height: 28px;
      border: 1px solid #d9d9d9;
      border-radius: 4px;
    }
  }

  .list{
    overflow-y: scroll;
    height:calc(100vh - 370px);
    margin-top: 10px;
  }
  ::-webkit-scrollbar {
    display: none;
  }
  .customer-list{
    /*display: flex;*/
    align-items: center;
    border-top: 1px solid #d9d9d9;
    .name{
      font-size: 13px;
      color: #161616;
      line-height: normal;
      margin-top: 5px;
    }
    .contact{
      margin-top: 5px;
      font-size: 12px;
      color: #a39f9f;
      margin-bottom: 5px;
    }
  }
</style>